// 管理员管理
import { commonEditForm, commonQuery, commonVisible, commonOptions, commonRule } from '@/api/config';
export const sysUserMixin = {
    data() {
        var validatePassword = (rule, value, callback) => {
            if (value === undefined) {
                callback(new Error("请输入密码"));
            } else {
                if (this.form.password !== "") {
                    callback();
                }
            }
        };
        var validateConfirmPassword = (rule, value, callback) => {
            if (value === "") {
                callback(new Error("请再次输入密码"));
            } else if (value !== this.form.password) {
                callback(new Error("两次输入密码不一致!"));
            } else {
                callback();
            }
        };
        return {
            roleList: [],
            selectRoles: [],
            value: [],
            title: "",
            increaseFlag: false,
            editFlag: false,
            deptList: [
                {
                    value: "",
                    label: "",
                    children: [
                        {
                            value: "",
                            label: ""
                        }
                    ]
                }
            ],
            query: Object.assign({}, commonQuery, { username: "" }),
            tableData: [
                {
                    id: '',
                    username: '',
                    deptName: '',
                    email: '',
                    mobile: '',
                    status: '',
                    createDate: '',
                    realName: '',
                    headUrl: '',
                    deptId: '',
                    superAdmin: '',
                    roleIdList: '',
                    gender: ''
                }
            ],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {
                username: "",
                password: "",
                confirmPassword: "",
                realName: "",
                gender: "",
                email: "",
                mobile: "",
                status: "",
                deptName: ""
            }, // 无角色配置和所属部门
            rules: {
                password: [
                    { required: true, validator: validatePassword, trigger: "blur" }
                ],
                confirmPassword: [
                    {
                        required: true,
                        validator: validateConfirmPassword,
                        trigger: "blur"
                    }
                ]
            },
            idx: -1,
            ids: []
        };
    },
    methods: {
        // 获取数据
        getSysUser({
            order = this.query.order,
            orderField = this.query.orderField,
            page = this.query.pageIndex,
            limit = this.query.pageSize,
            username = this.query.name,
            _t = Date.now()
        }) {
            this.$axios
                .get("/sys/user/page", {
                    params: {
                        order,
                        orderField,
                        page,
                        limit,
                        username,
                        _t
                    }
                })
                .then(res => {
                    if (res.status !== 200) {
                        return this.$message.error("获取数据失败");
                    }
                    var data = res.data.data;
                    this.pageTotal = data.total || 50;
                    var dataList = data.list;

                    for (var i = 0; i < dataList.length; i++) {
                        this.tableData.push({
                            id: dataList[i].id,
                            username: dataList[i].username,
                            deptName: dataList[i].deptName,
                            email: dataList[i].email,
                            mobile: dataList[i].mobile,
                            status: dataList[i].status,
                            createDate: dataList[i].createDate,
                            realName: dataList[i].realName,
                            headUrl: dataList[i].headUrl,
                            deptId: dataList[i].deptId,
                            superAdmin: dataList[i].superAdmin,
                            roleIdList: dataList[i].roleIdList,
                            gender: dataList[i].gender
                        });
                    }

                })
                .catch(err => {
                    return this.$message.error(err.$message);
                });
        }
    }
}
export const sysDeptMixin = {
    data() {
        return {
            ...commonVisible,
            rules: {
                name: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                parentName: [{ required: true, message: "必填不能为空", trigger: "blur" }]
            },
            editForm: {
                id: "",
                pid: "",
                children: [],
                name: "",
                sort: 0,
                createDate: null,
                parentName: null,
                options: [
                    {
                        value: "",
                        label: "",
                        children: []
                    }

                ]
            },
            addForm: {
                name: "",
                parentName: "",
                sort: 0,
                pid: "",
                options: [
                    {
                        value: "",
                        label: "",
                        children: []
                    }
                ]
            },
            tableData: [
                {
                    id: 1,
                    pid: "0",
                    name: "",
                    sort: 1,
                    parentName: null,
                    createDate: null,
                    show: false,
                    children: []
                }
            ],
        };
    },
    methods: {
        getJSData({ order = "", orderField = "", _t = Date.now() }) {
            this.$axios
                .get("/sys/dept/list", {
                    params: {
                        order,
                        orderField,
                        _t
                    }
                })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg);
                    }
                    function myMap(array) {
                        array.map(item => {
                            item.show = false;
                            if (item.children && item.children.length) {
                                return myMap(item.children);
                            }
                        });
                    }
                    myMap(res.data);
                    this.tableData = res.data;
                    // console.log(this.tableData);
                })
                .catch(() => { });
        }
    }
}

export const sysRoleMixin = {
    data() {
        return {
            defaultProps: {
                children: "children",
                label: "label"
            },
            query: {
                order: "",
                orderField: "",
                pageIndex: 1,
                pageSize: 20,
                name: ""
            },
            tableData: [
                {
                    id: "",
                    name: "",
                    remark: "",
                    createDate: "",
                    menuIdList: null,
                    deptIdList: null
                }
            ],

            addRuleForm: {
                name: "",
                remark: "",
                // 菜单授权
                menuAuthorizeList: [
                    {
                        id: "",
                        label: "",
                        children: []
                    }
                ],
                // 数据授权
                dataAuthorizeList: [
                    {
                        id: "",
                        label: "",
                        children: []
                    }
                ],
                dataAuthorizeIdList: [],
                menuAuthorizeIdList: []
            },
            rules: {
                name: [commonRule],
                menuAuthorizeList: [],
                dataAuthorizeList: []
            },
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            pageTotal: 0,
            editForm: {
                id: "",
                name: "",
                remark: ""
            },
            menuAuthorizeSelectChecked: [], // 菜单授权
            dataAuthorizeSelectChecked: [], // 数据授权,
            idx: -1,
            ids: []
        };
    },
    methods: {
        // 获取数据
        getSysRoleData({
            order = this.query.order,
            orderField = this.query.orderField,
            page = this.query.pageIndex,
            limit = this.query.pageSize,
            name = this.query.name,
            _t = Date.now()
        }) {
            this.$axios
                .get("/sys/role/page", {
                    params: {
                        order,
                        orderField,
                        page,
                        limit,
                        name,
                        _t
                    }
                })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg);
                    }
                    this.pageTotal = res.data.total || 50;
                    this.tableData = res.data.list;
                })
                .catch(() => { });
        }
    }
}